package com.newtouch.bxzs.business.train.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.newtouch.bxzs.business.train.mapper.UserCourseLearningRecordMapper;
import com.newtouch.bxzs.business.train.model.vo.UserCourseLearningRecordVO;
import com.newtouch.bxzs.business.train.service.UserCourseLearningRecordService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.UserCourseLearningRecord;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 用户课程学习记录Service实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/23 17:29</td>
 * <td>dingqianbin</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author dingqianbin
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class UserCourseLearningRecordServiceImpl implements UserCourseLearningRecordService {

    private static final Log logger = LogFactory.getLog(UserCourseLearningRecordServiceImpl.class);

    @Autowired
    UserCourseLearningRecordMapper userCourseLearningRecordMapper;

    @Override
    public Integer addUserCourseLearningRecord(UserCourseLearningRecordVO userCourseLearningRecordVO) {
        /* 检查必传项 */
        if (StringUtils.isEmpty(userCourseLearningRecordVO.getUserCourseId())) {
            logger.error("新增用户课程学习记录用户课程ID不能为空！");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        UserCourseLearningRecord userCourseLearningRecord = new UserCourseLearningRecord();
        BeanUtil.copyProperties(userCourseLearningRecordVO, userCourseLearningRecord);
        Date date = new Date();
        String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
        userCourseLearningRecord.setCreateUserId(userId);
        userCourseLearningRecord.setCreateTime(date);
        userCourseLearningRecord.setUpdateUserId(userId);
        userCourseLearningRecord.setUpdateTime(date);
        return userCourseLearningRecordMapper.insert(userCourseLearningRecord);
    }
}
